package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import bayesian.Bayesian;

/**
 * @author Xu,
 * @data 2020/12/6，
 * @time 14:12
 * <6.4,2.7,5.3,1.8,>  ---> Iris-virginica 2
<4.5,2.3,1.3,0.3,>  ---> Iris-setosa 0
<5.5,2.6,4.4,1.2,>  ---> Iris-versicolor 1
 */
public class Main {

    private static ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
    private static String path = "./src/iris.txt";
    public static void main(String[] args) {
        readData(path);
        //对缺失值进行处理，但文件data.txt不处理，可能对缺失值进行另一种方法处理
        dealWithData();
        System.out.println("对数据进行朴素贝叶斯分类：");
        Bayesian bayesian = new Bayesian("6.4","2.7","5.3","1.8",data);
        bayesian.calculate_Bayesian();
        Bayesian bayesian1 = new Bayesian("4.5","2.3", "1.3", "0.3",data);
        bayesian1.calculate_Bayesian();
        Bayesian bayesian2 = new Bayesian("5.5","2.6","4.4","1.2",data);
        bayesian2.calculate_Bayesian();
        Bayesian bayesian3 = new Bayesian("5.1","2.5","3.0","1.1",data);
        bayesian3.calculate_Bayesian();
        Bayesian bayesian4 = new Bayesian("6.7","3.0","5.0","1.7",data);
        bayesian4.calculate_Bayesian();
        //5.9,3.0,5.1,1.8,Iris-virginica
        Bayesian bayesian5 = new Bayesian("5.9","3.0","5.1","1.8",data);
        bayesian5.calculate_Bayesian();




        //show();
    }

    /**
     * -1代表缺失值，通过计算属性的平均值作为缺失值的填补值。
     */
    private static void dealWithData() {
        double sum1 = 0.0, sum2 = 0.0, sum3 = 0.0, sum4 = 0.0;
        double value1, value2, value3, value4;
        int len1 = data.size(),len2 = data.size(),len3 = data.size(),len4 = data.size();
        for (int i=0; i<data.size(); i++){
            if (!data.get(i).get(0).equals("-1")){
                sum1 += Double.parseDouble(data.get(i).get(0));
                len1--;
            }
            if (!data.get(i).get(1).equals("-1")){
                sum2 += Double.parseDouble(data.get(i).get(1));
                len2--;
            }
            if (!data.get(i).get(2).equals("-1")){
                sum3 += Double.parseDouble(data.get(i).get(2));
                len3--;
            }
            if (!data.get(i).get(3).equals("-1")){
                sum4 += Double.parseDouble(data.get(i).get(3));
                len4--;
            }
        }
        value1 = sum1/(len1*1.0);
        value2 = sum2/(len2*1.0);
        value3 = sum3/(len3*1.0);
        value4 = sum4/(len4 *1.0);
        for (int j=0; j<data.size(); j++){
            if (data.get(j).get(0).equals("-1")){
                data.get(j).set(0,Double.toString(value1));
            }
            if (data.get(j).get(1).equals("-1")){
                data.get(j).set(1,Double.toString(value2));
            }
            if (data.get(j).get(2).equals("-1")){
                data.get(j).set(2,Double.toString(value3));
            }
            if (data.get(j).get(3).equals("-1")){
                data.get(j).set(3,Double.toString(value4));
            }
        }
    }


    /**
     * 导入数据到data
     * @param path
     * @return
     */
    public  static ArrayList<ArrayList<String>> readData(String path){
        ArrayList<String> d = null;
        ArrayList<ArrayList<String>> t = new ArrayList<ArrayList<String>>();

        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(path)));
            BufferedReader br = new BufferedReader(isr);
            String str = null;
            while((str = br.readLine()) != null){
                d = new ArrayList<String>();
                String[] str1 = str.split(",");
                for(int i = 0; i < str1.length ; i++) {
                    d.add(str1[i]);
                }
                t.add(d);
            }
            data = t;
            br.close();
            isr.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取文件内容出错！");
        }
        return t;
    }

    /**
     * 遍历data
     */
    public static void show(){
        for(int i=0;i<data.size();i++){
            System.out.println(data.get(i));
        }
    }

}
